package com.jk11a.shangdaast.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;
import android.widget.Toast;

import com.jk11a.shangdaast.library.Book;
import com.jk11a.shangdaast.library.Reader;


public class LibraryDao {
	
	public Reader getReader(int id){
		Reader reader=new Reader();
		try{
			String urlString= "http://10.21.239.9:8080/ShangDaAstServer/getreader";
			HttpPost httpPost = new HttpPost(urlString);  
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
			   if (httpResponse.getStatusLine().getStatusCode() == 200) { 
					 String retSrc = EntityUtils.toString(httpResponse.getEntity());
					 JSONObject result = new JSONObject( retSrc);
					 reader.setName(result.getString("name"));
					 reader.setCardid(result.getString("cardid"));
					 reader.setOrderbook(result.getString("orderbook"));
					 reader.setOveredbook(result.getString("overedbook"));
					 reader.setOveringbook(result.getString("overingbook"));
					 reader.setTotalbook(result.getString("totalbook"));
					 reader.setWeituobook(result.getString("weituobook"));
			   }
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.print("total book"+reader.getTotalbook());
		return reader;
		
	}
	public List<Book> getPopBook(){
		List<Book> list=new ArrayList<Book>();

		try {
			String urlString= "http://10.21.239.9:8080/ShangDaAstServer/loadpopbook";
			HttpPost httpPost = new HttpPost(urlString);  
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);  
			 //若状态码为200 ok
			   if (httpResponse.getStatusLine().getStatusCode() == 200) { 
				   //读返回数据
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 String flag=(String)result.get("flag");
			 JSONArray jsonArray = result.getJSONArray("popbook");
			 int iSize = jsonArray.length();  
			 for (int i = 0; i < iSize; i++) {  
				 JSONObject jsonObj = jsonArray.getJSONObject(i);
					Book book=new Book();
					//book.setId(Integer.parseInt(jsonObj.getJSONObject("popnum").toString()));
					book.setBookid(jsonObj.getString("bookid"));
					book.setBookname(jsonObj.getString("bookname"));
					book.setWriter(jsonObj.getString("writer"));
					book.setPublishtime(jsonObj.getString("publishtime"));
					book.setPublisher(jsonObj.getString("publisher"));
					book.setSynopsis(jsonObj.getString("synopsis"));
					list.add(book);
			 }
			   }
			   else {
				Log.i("getpop failed", "getpop failed");
			}	 
		}catch(Exception e){
			e.printStackTrace();
		}
	
		return list;
	
	}
}
